package com.ctg.itrdc.sysmgr.portal.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ctg.itrdc.pasp.context.service.BaseService;
import com.ctg.itrdc.pasp.core.PageInfo;
import com.ctg.itrdc.sysmgr.portal.api.dto.ContactsInfoDTO;
import com.ctg.itrdc.sysmgr.portal.api.dto.PrivilegeDTO;
import com.ctg.itrdc.sysmgr.portal.api.dto.StaffDTO;
import com.ctg.itrdc.sysmgr.portal.api.service.IStaffService;
import com.ctg.itrdc.sysmgr.portal.common.constant.DictEnum.StatusCd;
import com.ctg.itrdc.sysmgr.portal.data.IContactsInfoDao;
import com.ctg.itrdc.sysmgr.portal.data.IStaffContactInfoDao;
import com.ctg.itrdc.sysmgr.portal.data.IStaffDao;
import com.ctg.itrdc.sysmgr.portal.data.entity.ContactsInfo;
import com.ctg.itrdc.sysmgr.portal.data.entity.Staff;
import com.ctg.itrdc.sysmgr.portal.data.entity.StaffContactInfo;
import com.ctg.itrdc.sysmgr.portal.service.utils.BeanUtils;

@Service("staffService")
public class StaffServiceImpl extends BaseService<Staff, Long, StaffDTO>
		implements IStaffService {

	@Resource
	private IContactsInfoDao contactsInfoDao;
	@Resource
	private IStaffContactInfoDao staffContactInfoDao;
	@Resource
	private IStaffDao staffDao;

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public PageInfo<StaffDTO> queryPageInfoByOrg(StaffDTO dto, int currentPage,
			int perPageNum) {
		Staff entity = BeanUtils.applyIfNotException(dto, Staff.class);
		// 查询当前及下级组织的员工
		// List<OrganizationDTO> orgDTOList =
		// organizationService.selectMainAndSubByOrgId(dto.getOrgId());
		// Long[] orgIds = new Long[orgDTOList.size()];
		// for (int i = 0; i< orgDTOList.size();i++){
		// orgIds[i] = orgDTOList.get(i).getOrgId();
		// }
		// entity.setOrgId(null);
		Long orgId = entity.getOrgId();
		PageInfo pageInfo = null;
		if (orgId == null) {
			pageInfo = staffDao.selectByExampleAndOrgIds(entity, currentPage,
					perPageNum);
		} else {
			pageInfo = staffDao.selectByExampleAndOrgIds(entity, currentPage,
					perPageNum, orgId);
		}

		List<StaffDTO> list = BeanUtils.copyListNotException(
				pageInfo.getList(), StaffDTO.class);
		for (StaffDTO staffDTO : list) {
			setContactsInfo(staffDTO);
		}
		pageInfo.setList(list);
		return pageInfo;
	}

	@Override
	public StaffDTO selectByPrimaryKey(StaffDTO dto) {
		StaffDTO staffDTO = super.selectByPrimaryKey(dto);
		if (staffDTO != null)
			setContactsInfo(staffDTO);
		return staffDTO;
	}
	
	@Transactional
	@Override
	public Long insert(StaffDTO staffDTO) {
		// 插入staff
		long staffId = super.insert(staffDTO);
		// 去掉 员工编号自动生成的逻辑 xkw 20160808
		// String staffCode = RandomCodeUtil.gen(RandomCodeUtil.SYSTEM_STAFF,
		// staffId);
		// StaffDTO tmp = new StaffDTO();
		// tmp.setId(staffId);
		// tmp.setStaffCode(staffCode);
		// sysmgrBaseService.updateNotNullFieldsByPrimaryKey(tmp);

		// 插入contacts_info
		ContactsInfoDTO contactsInfoDTO = staffDTO.getContactsInfo();
		if(contactsInfoDTO != null) {
			String mobilePhone = contactsInfoDTO.getMobilePhone();
			if(mobilePhone != null && !mobilePhone.equals("")) {
				ContactsInfo contactsInfo = BeanUtils.applyIfNotException(contactsInfoDTO, ContactsInfo.class);
				contactsInfo.setStatusCd(StatusCd.S0A.toString());
				long connectId = contactsInfoDao.insert(contactsInfo);
				// 插入staff_contact_info
				StaffContactInfo sci = new StaffContactInfo();
				sci.setConnectId(connectId);
				sci.setStaffId(staffId);
				staffContactInfoDao.insert(sci);
			}
		}
		return staffId;
	}

	@Transactional
	@Override
	public int updateByPrimaryKey(StaffDTO staffDTO) {
		StaffContactInfo staffContactInfo = new StaffContactInfo();
		staffContactInfo.setStaffId(staffDTO.getStaffId());
		List<StaffContactInfo> list = staffContactInfoDao
				.selectByExample(staffContactInfo);
		// 更新 contacts_info
		ContactsInfoDTO contactsInfoDTO = staffDTO.getContactsInfo();
		ContactsInfo contactsInfo = BeanUtils.applyIfNotException(contactsInfoDTO, ContactsInfo.class);
		if(contactsInfo != null && contactsInfo.getMobilePhone() != null) {
			// 如果之前就存在联系方式
			if (CollectionUtils.isNotEmpty(list)) {
				contactsInfo.setId(list.get(0).getId());
				contactsInfoDao.updateByPrimaryKey(contactsInfo);
			} else {// 联系方式之前没有，在更新数据时加入
				Long id = contactsInfoDao.insert(contactsInfo);
				staffContactInfo.setConnectId(id);
				staffContactInfoDao.insert(staffContactInfo);
			}
		}

		// 更新 staff
		return super.updateByPrimaryKey(staffDTO);
	}

	@Transactional
	@Override
	public int deleteByPrimaryKey(StaffDTO staffDTO) {

		staffDTO = this.selectByPrimaryKey(staffDTO);
		Long staffId = staffDTO.getStaffId();
		ContactsInfoDTO contactsInfoDTO = staffDTO.getContactsInfo();
		if (contactsInfoDTO != null) {
			ContactsInfo contactsInfo = BeanUtils.applyIfNotException(contactsInfoDTO, ContactsInfo.class);
			Long connectId = contactsInfo.getConnectId();
			if (connectId != null) {
				// 删除contacts_info
				contactsInfoDao.deleteByPrimaryKey(contactsInfo);
				// 删除staff_contact_info
				StaffContactInfo sci = new StaffContactInfo();
				sci.setStaffId(staffId);
				sci.setConnectId(connectId);
				List<StaffContactInfo> sciList = staffContactInfoDao.selectByExample(sci);
				staffContactInfoDao.jdbcBatchDeleteByPrimary(sciList);
			}
		}
		// 删除staff
		return super.deleteByPrimaryKey(staffDTO);
	}
	
	@Transactional
	public int deleteByPrimaryKey(Long id) {
		return super.deleteByPrimaryKey(id);
	}
	
	/**
	 * 不推荐使用
	 */
	@Transactional
	@Override
	public int[] batchDeleteByPrimaryKey(List<StaffDTO> dtoList) {

		for (StaffDTO staffDTO : dtoList) {

			staffDTO = selectByPrimaryKey(staffDTO);
			Long staffId = staffDTO.getStaffId();
			ContactsInfoDTO contactsInfoDTO = staffDTO.getContactsInfo();
			if (contactsInfoDTO != null) {
				ContactsInfo contactsInfo = BeanUtils.applyIfNotException(contactsInfoDTO, ContactsInfo.class);
				Long connectId = contactsInfo.getConnectId();
				if (connectId != null) {
					// 删除contacts_info
					contactsInfoDao.deleteByPrimaryKey(contactsInfo);
					// 删除staff_contact_info
					StaffContactInfo sci = new StaffContactInfo();
					sci.setStaffId(staffId);
					sci.setConnectId(connectId);
					staffContactInfoDao.deleteByPrimaryKey(sci);
				}
			}
		}
		// 删除staff
		List<Staff> entities = BeanUtils.copyListNotException(dtoList,
				Staff.class);
		return staffDao.jdbcBatchDeleteByPrimary(entities);
	}

	private void setContactsInfo(StaffDTO staffDTO) {
		if (staffDTO == null)
			return;
		Long staffId = staffDTO.getStaffId();
		StaffContactInfo sci = new StaffContactInfo();
		sci.setStaffId(staffId);
		List<StaffContactInfo> sciList = staffContactInfoDao
				.selectByExample(sci);
		if (CollectionUtils.isNotEmpty(sciList)) {
			sci = sciList.get(0);
			ContactsInfo ci = new ContactsInfo();
			ci.setConnectId(sci.getConnectId());
			ci = contactsInfoDao.selectByPrimaryKey(ci);
			if(ci == null) {// 联系方式可为空
				return;
			}
			ContactsInfoDTO dto = BeanUtils.applyIfNotException(ci, ContactsInfoDTO.class);
			staffDTO.setContactsInfo(dto);
		}
	}
}
